package com.itheima.controller;


import com.itheima.anno.Log;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import jakarta.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/depts")
@RestController
public class DeptController {

    private static final Logger log = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;
    //@RequestMapping(value = "/depts", method = RequestMethod.GET)
    //查询部门信息
    @Log
    @GetMapping
    public Result list(){
        //System.out.println("查询全部的部门数据：");
        log.info("查询全部的部门数据：");
        List<Dept> deptlist = deptService.findAll();
        return Result.success(deptlist);
    }

    //删除部门信息
//    @DeleteMapping("/depts")
//    public Result delete(@RequestParam("id") Integer deptId){
//        System.out.println("删除部门数据：" + deptId);
//        return Result.success();
//    }

    @Log
    @DeleteMapping
    public Result delete(Integer id){
        //System.out.println("删除部门数据：" + id);
        log.info("删除部门数据：{}" ,id);
        deptService.deleteByID(id);
        return Result.success();
    }

    //添加部门信息
    @PostMapping
    public Result add(@RequestBody Dept dept){
        //System.out.println("添加部门数据：" + dept);
        log.info("添加部门数据：{}" ,dept);
        deptService.add(dept);
        return Result.success();
    }

    @Log
    //根据ID查询部门信息
    @GetMapping("/{id}")
    public Result getinfo(@PathVariable Integer id){
        //System.out.println("查询部门数据：" + id);
        log.info("查询部门数据：{}" ,id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
    //修改部门信息
    @PutMapping
    public Result update(@RequestBody Dept dept){
        //System.out.println("修改部门数据：" + dept);
        log.info("修改部门数据：{}" ,dept);
        deptService.update(dept);
        return Result.success();
    }

}
